package com.sean.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LongRequsetSample
 */
public class LongRequsetSample extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private final int TIMEOUT = 30;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public LongRequsetSample() {
	super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    @Override
    protected void doGet(HttpServletRequest request,
	    HttpServletResponse response) throws ServletException, IOException {
	doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    @Override
    protected void doPost(HttpServletRequest request,
	    HttpServletResponse response) throws ServletException, IOException {
	int timeout = TIMEOUT;
	String parameter = request.getParameter("timeout");
	if (parameter != null) {
	    try {
		timeout = Integer.parseInt(parameter);
	    } catch (Exception e) {

	    }
	}
	// Long request.
	try {
	    Thread.sleep(timeout * 1000);
	} catch (InterruptedException e) {
	    e.printStackTrace();
	}
	RequestDispatcher dispather = request
		.getRequestDispatcher("long.jsp");
	dispather.forward(request, response);
	response.flushBuffer();
    }

}
